package demo04_other

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo02_jdbc {

  def userOperator() = {

    var conn:Connection = null
    var ps:PreparedStatement = null
    var rs:ResultSet = null

    try{
      Class.forName("com.mysql.jdbc.Driver")
      conn = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/bookshop?useSSL=false",
        "root",
        "root"
      );
      ps = conn.prepareStatement("select * from tb_user")

      rs = ps.executeQuery()

      while(rs.next()){
        println(s"username:${rs.getString("user_name")}")
        println(s"realname:${rs.getString("real_name")}")
        println("=======================================")
      }
    }catch {
      case e:RuntimeException => e.printStackTrace()
    }finally {
      if(rs!=null) rs.close()
      if(ps!=null) ps.close()
      if(conn!=null) conn.close()
    }
  }

  def main(args: Array[String]): Unit = {
    userOperator()
  }

}
